/*
 * This file is part of aKoustiK.

 * aKoustiK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * aKoustiK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with aKoustiK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <KAction>
#include <KApplication>
#include <KIcon>
#include <KLocale>
#include <KStandardAction>
#include <QtCore/QDebug>

#include <storage/internalstorage.h>
#include <kore.h>

Kore* Kore::_instance = new Kore();

Kore &Kore::instance() {
    return *_instance;
}

Kore::Kore() {
    qDebug("Kore: constructor invoked");
}

Kore::~Kore() {
    // TODO do something to actually invoke it before real application quit
    qDebug("Kore: destructor invoked");
}

KActionCollection* Kore::actionCollection() {
    return mActionCollection;
}

Player* Kore::player() {
    return mPlayer;
}

Playlist* Kore::playlist() {
    return mPlaylist;
}

void Kore::preparePlaylist() {
    qDebug("Kore: creating Playlist...");
    mPlaylist = InternalStorage::loadCurrentPlaylist();
    connect(mPlaylist, SIGNAL(playlistChanged()), SLOT(playlistChanged()));
    qDebug("Kore: Playlist created.");
}

void Kore::preparePlayer() {
    qDebug("Kore: creating Player...");
    mPlayer = new Player(this);
    qDebug("Kore: Player created.");
}

void Kore::prepareActions() {
    qDebug("Kore: creating action collection...");
    mActionCollection = new KActionCollection(this);
    mActionCollection->addAction(KStandardAction::Quit, "quit", KApplication::kApplication(), SLOT(quit()));

    // previous:
    {
        KAction* prevAction = new KAction(KIcon("media-skip-backward"), i18n("Previous"), mActionCollection);
        mActionCollection->addAction("previous", prevAction);
        prevAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_Z));
        connect(prevAction, SIGNAL(triggered()), mPlayer, SLOT(previous()));
    }
    // play:
    {
        KAction* playAction = new KAction(KIcon("media-playback-start"), i18n("Play"), mActionCollection);
        mActionCollection->addAction("play", playAction);
        playAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_X));
        connect(playAction, SIGNAL(triggered()), mPlayer, SLOT(play()));
    }
    // pause:
    {
        KAction* pauseAction = new KAction(KIcon("media-playback-pause"), i18n("Pause"), mActionCollection);
        mActionCollection->addAction("pause", pauseAction);
        pauseAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_C));
        connect(pauseAction, SIGNAL(triggered()), mPlayer, SLOT(pause()));
    }
    // stop:
    {
        KAction* stopAction = new KAction(KIcon("media-playback-stop"), i18n("Stop"), mActionCollection);
        mActionCollection->addAction("stop", stopAction);
        stopAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_V));
        connect(stopAction, SIGNAL(triggered()), mPlayer, SLOT(stop()));
    }
    // next:
    {
        KAction* nextAction = new KAction(KIcon("media-skip-forward"), i18n("Next"), mActionCollection);
        mActionCollection->addAction("next", nextAction);
        nextAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_B));
        connect(nextAction, SIGNAL(triggered()), mPlayer, SLOT(next()));
    }
    // mute-unmute:
    {
        KAction* muteAction = new KAction(KIcon("audio-volume-muted"), i18n("Mute"), mActionCollection);
        mActionCollection->addAction("mute", muteAction);
        muteAction->setCheckable(true);
        //connect(mPlayer, SIGNAL(mutedChanged(bool)), muteAction, SLOT(setChecked(bool)));
        muteAction->setGlobalShortcut(KShortcut(Qt::META + Qt::Key_M));
        connect(muteAction, SIGNAL(triggered(bool)), mPlayer, SLOT(mute(bool)));
    }
    // current-remove:
    {
        KAction* removeCurrentAction = new KAction(KIcon("list-remove"), i18n("Remove current track"), mActionCollection);
        mActionCollection->addAction("remove-current", removeCurrentAction);
        connect(removeCurrentAction, SIGNAL(triggered()), SLOT(removeCurrentItemSignal()));
    }
    qDebug("Kore: actionCollection created.");
}

void Kore::prepareTray() {
    qDebug("Kore: creating Tray...");
    tray = new Tray(this);
    qDebug("Kore: Tray created.");
}

void Kore::launch() {
    qDebug("Kore: launch procedure started...");
    preparePlaylist();
    preparePlayer();
    prepareActions();
    prepareTray();
    qDebug("Kore: launch procedure complete.");
}

void Kore::removeCurrentItemSignal() {
    int currentItem = mPlayer->current();
    mPlaylist->removeItem(currentItem);
}

void Kore::playlistChanged() {
    InternalStorage::store();
}
